package com.boss.salary.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.boss.asset.service.IAssetInfoService;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.constant.ApiElementConstant;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.IPersonInfoDao;
import com.boss.salary.entity.PersonExt;
import com.boss.salary.entity.PersonInfo;
import com.boss.salary.service.IPersonExtService;
import com.boss.salary.service.IPersonInfoService;
import com.boss.salary.vo.PersonInfoVO;
import com.boss.sys.ca.user.dto.UserDTO;
import com.boss.sys.utils.SysUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PersonInfoServiceImpl implements IPersonInfoService {


    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(PersonInfoServiceImpl.class);

    @Autowired
    private IPersonInfoDao dao;

    @Autowired
    private IPersonExtService iPersonExtService;
    @Autowired
    private IAssetInfoService iAssetInfoService;
    
    @Override
    public PageDTO<PersonInfo> queryPersonInfoPage(PaginationDTO paginationDTO, Map<String, Object> queryParam) throws AppRuntimeException {
        List<PersonInfo> list = dao.queryPersonInfoPage(paginationDTO, queryParam);
        int total = dao.queryPersonInfoTotal(queryParam);
        PageDTO<PersonInfo> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }
    /**
     * 分页查询 工资是否停发数据
     * @param queryParam
     * @return
     */
    @Override
    public PageDTO<PersonInfo> queryStopPaySalary(PaginationDTO paginationDTO, Map<String, Object> queryParam, UserDTO userDTO,String tokenid) throws AppRuntimeException {
        JSONArray jsonArray =iAssetInfoService.getAuthAgency(userDTO, ApiElementConstant.AGENCY,tokenid);
        List<String> agencyList=new ArrayList<>();
        if(!CollectionUtils.isEmpty(jsonArray)&&jsonArray!=null){
            for(Object json : jsonArray){
                agencyList.add(json.toString());
            }
        }
        List<PersonInfo> list = dao.queryStopPaySalaryPage(paginationDTO, queryParam,agencyList);
        int total = dao.queryStopPaySalaryTotal(queryParam,agencyList);
        PageDTO<PersonInfo> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePersonInfo(PersonInfo personInfo) {
        // 新增人员信息表数据
        personInfo.setId(SysUtils.generateGuid());
        personInfo.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        personInfo.setIsDeleted(SystemDictConstant.IS_FALSE);
        dao.savePersonInfo(personInfo);
        // 新增人员信息拓展表数据
        PersonExt personExt = new PersonExt();
//        BeanUtils.copyProperties(personExt,personInfo);
        personExt.setPersonId(personInfo.getId());
        personExt.setPersonAddress(personInfo.getPersonAddress());
        personExt.setAgencyId(personInfo.getAgencyId());
        personExt.setDeptId(personInfo.getDeptId());
        personExt.setAgencyCode(personInfo.getAgencyCode());
        personExt.setIdenNo(personInfo.getIdenNo());//20210304新增
        personExt.setState(personInfo.getState());//20210304新增
        personExt.setEnterAgencyDate(personInfo.getEnterAgencyDate());
        personExt.setRetireDate(personInfo.getRetireDate());
        personExt.setPersonTypeCode(personInfo.getPersonTypeCode());
        personExt.setPersonIdeCode(personInfo.getPersonIdeCode());
        personExt.setEndDate(personInfo.getEndDate());
        personExt.setDeadDate(personInfo.getDeadDate());
        personExt.setRemarks(personInfo.getRemarks());
        personExt.setSalaCardNo(personInfo.getSalaCardNo());
        personExt.setSalaCardBank(personInfo.getSalaCardBank());
        personExt.setGovCredCardNo(personInfo.getGovCredCardNo());
        personExt.setGovCredCardBank(personInfo.getGovCredCardBank());
        personExt.setStartDate(personInfo.getStartDate());
        personExt.setMeritPayCode(personInfo.getMeritPayCode());
        personExt.setSchRecCode(personInfo.getSchRecCode());
        personExt.setPosCode(personInfo.getPosCode());
        personExt.setGrCode(personInfo.getGrCode());
        personExt.setTecGrCode(personInfo.getTecGrCode());
        personExt.setIsAuth(personInfo.getIsAuth());
        personExt.setSalaGrCode(personInfo.getSalaGrCode());
        personExt.setPersonState(personInfo.getPersonState());
        personExt.setChangeState(personInfo.getChangeState());
        personExt.setPerSourceCode(personInfo.getPerSourceCode());
        personExt.setIsSpecialPost(personInfo.getIsSpecialPost());
        personExt.setIsInsurance(personInfo.getIsInsurance());
        personExt.setIsUniSala(personInfo.getIsUniSala());
        iPersonExtService.savePersonExt(personExt);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonInfo(PersonInfo personInfo) {
        // 更新人员 表数据
        personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        dao.updatePersonInfo(personInfo);
        // 更新人员信息拓展表数据
        PersonExt personExt = new PersonExt();
        personExt.setPersonId(personInfo.getId());
        personExt.setPersonAddress(personInfo.getPersonAddress());
        personExt.setAgencyId(personInfo.getAgencyId());
        personExt.setDeptId(personInfo.getDeptId());
        personExt.setAgencyCode(personInfo.getAgencyCode());
        personExt.setEnterAgencyDate(personInfo.getEnterAgencyDate());
        personExt.setRetireDate(personInfo.getRetireDate());
        personExt.setPersonTypeCode(personInfo.getPersonTypeCode());
        personExt.setPersonIdeCode(personInfo.getPersonIdeCode());
        personExt.setEndDate(personInfo.getEndDate());
        personExt.setDeadDate(personInfo.getDeadDate());
        personExt.setRemarks(personInfo.getRemarks());
        personExt.setSalaCardNo(personInfo.getSalaCardNo());
        personExt.setSalaCardBank(personInfo.getSalaCardBank());
        personExt.setGovCredCardNo(personInfo.getGovCredCardNo());
        personExt.setGovCredCardBank(personInfo.getGovCredCardBank());
        personExt.setStartDate(personInfo.getStartDate());
        personExt.setMeritPayCode(personInfo.getMeritPayCode());
        personExt.setSchRecCode(personInfo.getSchRecCode());
        personExt.setPosCode(personInfo.getPosCode());
        personExt.setGrCode(personInfo.getGrCode());
        personExt.setTecGrCode(personInfo.getTecGrCode());
        personExt.setIsAuth(personInfo.getIsAuth());
        personExt.setSalaGrCode(personInfo.getSalaGrCode());
        personExt.setPersonState(personInfo.getPersonState());
        personExt.setChangeState(personInfo.getChangeState());
        personExt.setPerSourceCode(personInfo.getPerSourceCode());
        personExt.setIsSpecialPost(personInfo.getIsSpecialPost());
        personExt.setIsInsurance(personInfo.getIsInsurance());
        personExt.setIsUniSala(personInfo.getIsUniSala());
        iPersonExtService.updatePersonExt(personExt);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePersonInfoList(List<PersonInfo> personInfos) {
        for (PersonInfo personInfo: personInfos) {
            // 更新人员 表数据
            personInfo.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
            dao.updatePersonInfo(personInfo);
            // 更新人员信息拓展表数据
            PersonExt personExt = new PersonExt();
            personExt.setPersonId(personInfo.getId());
            personExt.setPersonAddress(personInfo.getPersonAddress());
            personExt.setAgencyId(personInfo.getAgencyId());
            personExt.setDeptId(personInfo.getDeptId());
            personExt.setAgencyCode(personInfo.getAgencyCode());
            personExt.setEnterAgencyDate(personInfo.getEnterAgencyDate());
            personExt.setRetireDate(personInfo.getRetireDate());
            personExt.setPersonTypeCode(personInfo.getPersonTypeCode());
            personExt.setPersonIdeCode(personInfo.getPersonIdeCode());
            personExt.setEndDate(personInfo.getEndDate());
            personExt.setDeadDate(personInfo.getDeadDate());
            personExt.setRemarks(personInfo.getRemarks());
            personExt.setSalaCardNo(personInfo.getSalaCardNo());
            personExt.setSalaCardBank(personInfo.getSalaCardBank());
            personExt.setGovCredCardNo(personInfo.getGovCredCardNo());
            personExt.setGovCredCardBank(personInfo.getGovCredCardBank());
            personExt.setStartDate(personInfo.getStartDate());
            personExt.setMeritPayCode(personInfo.getMeritPayCode());
            personExt.setSchRecCode(personInfo.getSchRecCode());
            personExt.setPosCode(personInfo.getPosCode());
            personExt.setGrCode(personInfo.getGrCode());
            personExt.setTecGrCode(personInfo.getTecGrCode());
            personExt.setIsAuth(personInfo.getIsAuth());
            personExt.setSalaGrCode(personInfo.getSalaGrCode());
            personExt.setPersonState(personInfo.getPersonState());
            personExt.setChangeState(personInfo.getChangeState());
            personExt.setPerSourceCode(personInfo.getPerSourceCode());
            personExt.setIsSpecialPost(personInfo.getIsSpecialPost());
            personExt.setIsInsurance(personInfo.getIsInsurance());
            personExt.setIsUniSala(personInfo.getIsUniSala());
            personExt.setIsDeleted(personInfo.getIsDeleted());
            personExt.setIsStopSalary(personInfo.getIsStopSalary());
            iPersonExtService.updatePersonExt(personExt);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePersonInfo(String id) {
        dao.deletePersonInfo(id);
    }

    @Override
    public Map<String, Object> getStateNum(Map<String, Object> queryParam) {
        return dao.getStateNum(queryParam);
    }

    @Override
    public List<PersonInfo> getAllPersonInfo(String agencyId) {
        return dao.getAllPersonInfo(agencyId);
    }

    @Override
    public PersonInfoVO getOnePersonInfo(String personId) {
        return dao.selectOnePersonInfo(personId);
    }

}
